package com.cn.wanxi.service;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 第三阶段：
 * 数据要从文件里面来
 * 1.必须要有文件  创建文件
 * 2.必须要有数据  写入数据
 * 3.必须要读取数据  读文件
 * 4.封装数据
 * 5.返回数据
 * ---------------------------
 * 全部写在一个方法里面 ：缺点
 * 1.代码里太大
 * 2.不好维护
 * 3.臃肿（冗余）
 * ------------------
 * 每个方法只做单一事情（一件事情）,最好要写注释
 */
public class CompanyThreeService implements ICompanyService {

    //    魔法值
    private static final String DIRECTORY = "note";
    private static final String FILE_NAME = "company.txt";
    private static final   String SPLIT = "\n";
    @Override
    public CompanyModel getCompanyModel(String path) {
//        1.创建文件
//       path 相对地址
//        正常情况下  网址的端口号是：80
//        tomcat的端口号  8080
//        path = "E:\\wanxi\\step-3\\canyin03-0823\\out\\artifacts\\canyin03_0823_war_exploded\\";
        path = path + "/web/";
        File file = createNote(path, DIRECTORY, FILE_NAME);
//        2.写数据
        List<String> data = getDaoData();//数据应该属于什么数据类型?
        writeNote(file, data);
//        3.读数据
        Object object = readNote(file);
//        4.封装数据
        CompanyModel companyModel = setData(object);
//        5.返回数据
        return companyModel;
    }

    private List<String> getDaoData() {
//        如果数据量太大，全部写入到数组里面，会很麻烦
//        String[] data = {
//                CompanyDao.VERSION,
//                CompanyDao.NET,
//                CompanyDao.EMAIL,
//                CompanyDao.EMAIL,
//                CompanyDao.LOGO,
//                CompanyDao.LINE_PHONE
//        };
//        String[] data=new String[6];
//        return data;
//        数组和集合之间的区别？
//        泛型
//        泛型可以放基本数据类型吗？
        List<String> list = new ArrayList<>();
        list.add(CompanyDao.LOGO);
        list.add(CompanyDao.PHONE);
        list.add(CompanyDao.NET);
        list.add(CompanyDao.EMAIL);
        list.add(CompanyDao.LINE_PHONE);
        list.add(CompanyDao.ADDRESS);
        list.add(CompanyDao.VERSION);
        return list;
    }

    private CompanyModel setData(Object object) {
        return null;
    }

    private Object readNote(File file) {
        return null;
    }

    private void writeNote(File file,List<String> data) {
//        写入文件---输入输出
        try {
            OutputStream outputStream = new FileOutputStream(file);
//            CompanyDao companyDao = new CompanyDao();
//            CompanyDao.ADDRESS;
//            将dao层的字符串数据转换为整型

//            需要将写入的数据转为字符串数组
//            String[] data = {};
            for (int i = 0; i < data.size(); i++) {
                outputStream.write(data.get(i).getBytes());
                outputStream.write(SPLIT.getBytes());
            }
//            outputStream.write(CompanyDao.ADDRESS.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(CompanyDao.LINE_PHONE.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(CompanyDao.LOGO.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(CompanyDao.EMAIL.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(CompanyDao.NET.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(CompanyDao.VERSION.getBytes());


            /**
             * int a=1;
             * char c=a;
             *
             */
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

//    File directory = null;

    private File createNote(String path, String note, String fileName) {
//        就近原则
        File directory = new File(path, note);
        directory.mkdirs();
//
        File file = new File(directory, fileName);
//        int i = 0;
//        int b = 10 / i;
//        try {
//            file.createNewFile();
//
//            System.out.println(111);
//            System.exit(0);
//            return aa();
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println(22);
//        } finally {
//            System.out.println(333);
//        }
//        System.out.println(444);
//        return null;
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public File aa() {
        System.out.println(55);
        return null;
    }

    public static void main(String[] args) {
        new CompanyThreeService().getCompanyModel("E:\\wanxi\\step-3\\canyin03-0823\\");
//        System.out.println(note);
    }
}
